
from .tree_base import Tree


class BinaryTree(Tree):
    """Abstract base class representing a binary tree structure."""

    def left(self, p):
        """"Return a Position representing p's left child"""
        raise NotImplemented("must be implemented by subclass")

    def right(self, p):
        """Return a Postion representing p's right child"""
        raise NotImplemented("must be implemented by subclass")

    def sibling(self, p):
        """Return a position representing p's sibling"""
        parent = self.parent(p)
        if parent is None:
            return None
        else:
            if p == self.left(parent):
                return self.right(parent)
            else:
                return self.left(parent)

    def children(self, p):
        """Generate an iteration of position respresenting p's children"""
        if self.left(p) is not None:
            yield self.left(p)
        if self.right(p) is not None:
            yield self.right(p)

